package com.boby.lib_common

import android.app.Activity
import android.app.Application
import android.content.Context
import android.content.res.Configuration
import android.os.Handler
import android.os.HandlerThread
import android.view.Gravity
import com.boby.lib_common.extens.dp
import com.hjq.toast.ToastUtils
import com.hjq.toast.style.ToastWhiteStyle


/***
 *
 *   █████▒█    ██  ▄████▄   ██ ▄█▀       ██████╗ ██╗   ██╗ ██████╗
 * ▓██   ▒ ██  ▓██▒▒██▀ ▀█   ██▄█▒        ██╔══██╗██║   ██║██╔════╝
 * ▒████ ░▓██  ▒██░▒▓█    ▄ ▓███▄░        ██████╔╝██║   ██║██║  ███╗
 * ░▓█▒  ░▓▓█  ░██░▒▓▓▄ ▄██▒▓██ █▄        ██╔══██╗██║   ██║██║   ██║
 * ░▒█░   ▒▒█████▓ ▒ ▓███▀ ░▒██▒ █▄       ██████╔╝╚██████╔╝╚██████╔╝
 *  ▒ ░   ░▒▓▒ ▒ ▒ ░ ░▒ ▒  ░▒ ▒▒ ▓▒       ╚═════╝  ╚═════╝  ╚═════╝
 *  ░     ░░▒░ ░ ░   ░  ▒   ░ ░▒ ▒░
 *  ░ ░    ░░░ ░ ░ ░        ░ ░░ ░
 *           ░     ░ ░      ░  ░
 *
 * 通用的Application
 */

abstract class BaseApplication : Application() {

    companion object {
        private var instance: Application? = null
        fun instance() = instance ?: throw Throwable("instance 还未初始化完成")
        private var mHandler: Handler? = null
        @Volatile
        private var wrokHandler: Handler? = null

        fun getWorkHandler(): Handler? {
            if (wrokHandler == null) {
                synchronized(BaseApplication::class.java) {
                    if (wrokHandler == null) {
                        val thread = HandlerThread("main work handler")
                        thread.start()
                        wrokHandler = Handler(thread.looper)
                    }
                }
            }
            return wrokHandler
        }

        fun getHandler(): Handler? {
            return mHandler
        }
    }

    override fun attachBaseContext(base: Context) {
        super.attachBaseContext(base)

    }

    override fun onCreate() {
        super.onCreate()
        instance = this
        mHandler = Handler(mainLooper)
        initToast()
    }

    override fun onTerminate() {
        super.onTerminate()
    }

    private fun initToast(){
        ToastUtils.init(this)
        ToastUtils.initStyle(ToastWhiteStyle(this))
        ToastUtils.setGravity(Gravity.BOTTOM,0,120f.dp().toInt())
    }



}


